package com.lq.memo.service.impl;

import com.lq.memo.calendar.LunarCalendar;
import com.lq.memo.config.MemoConfig;
import com.lq.memo.entity.Memo;
import com.lq.memo.entity.MemoTimes;
import com.lq.memo.exception.MyException;
import com.lq.memo.service.CalendarService;
import com.lq.memo.service.MemoService;
import com.lq.memo.service.MemoTimesService;
import com.lq.memo.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName CalendarServiceImpl
 * @Description
 * @Author LQ
 * @Date 2021/12/14 23:03
 * @Version 1.0
 */
@Service
public class CalendarServiceImpl implements CalendarService {
    @Autowired
    LunarCalendar lunarCalendar;

    @Autowired
    MemoService memoService;
    @Autowired
    MemoConfig memoConfig;

    @Autowired
    MemoTimesService memoTimesService;

    @Override
    public Map<Integer, Map<String, Object>> getLunarCalendar(Map<String, Object> map) throws MyException {
        Integer year = Integer.valueOf(map.get("year").toString());
        Integer month = Integer.valueOf(map.get("month").toString());
        Integer days = Integer.valueOf(map.get("days").toString());
        Long userId = Long.valueOf(StringUtil.obj2Str(map.get("userId")));
        Memo queryMemo = new Memo();
        queryMemo.setUserId(userId);
        Map<Integer, Map<String, Object>> result = new HashMap<>();
        if (year < 1900) {
            throw new MyException("年份不能小于1900");
        }
        if (month < 1 || month > 12) {
            throw new MyException("月不能小于1或者大于12");
        }
        if (days > 31 || days < 1) {
            throw new MyException("月不能小于1或者大于31");
        }
        List<String> lettuceCalendars = new ArrayList<>();
        List<MemoTimes> allMemoTimes = memoTimesService.getAllMemoTimes();
        List<String> calendars = new ArrayList<>();
        for (int i = 1; i <= days; i++) {
            Map<String, Object> lunarMap = new HashMap<>();
            String lunarDay = lunarCalendar.getLunarDate(year, month, i, false);
            String lunarCalender = lunarCalendar.getLunarCalender(year, month, i);
            String lunarCalenderMonthDay = lunarCalendar.getLunarCalenderMonthDay(year, month, i);
            lunarMap.put("lunarDay", lunarDay);
            lunarMap.put("lunarCalender", lunarCalender);
            lunarMap.put("lunarCalenderMonthDay", lunarCalenderMonthDay);
            lunarMap.put("haveMemo", false);
            result.put(i, lunarMap);
            calendars.add(month + "-" + i);
            lettuceCalendars.add(lunarCalenderMonthDay.replace(memoConfig.getLeapMonthIdentifier(), ""));
        }
        queryMemo.setCalendars(calendars);
        queryMemo.setLettuceCalendars(lettuceCalendars);
        List<Memo> memoList = memoService.getMemoListByCalendar(queryMemo);
        for (int i = 1; i <= days; i++) {
            int finalI = i;
            Map<String, Object> stringObjectMap = result.get(finalI);
            List<Memo> memos = stringObjectMap.get("memo") != null ? (List<Memo>) stringObjectMap.get("memo") : new ArrayList<>();
            //List<MemoController> memos = new ArrayList<>();
            memoList.forEach(memo -> {
                Date memoDate = memo.getMemoDate();
                String memoDateFormat = new SimpleDateFormat("MM-dd").format(memoDate);
                setMemoTimes(memo,allMemoTimes);
                if (memo.getCalendarType() == 0) {
                    // 农历
                    if (StringUtils.equals(memoDateFormat, StringUtil.obj2Str(stringObjectMap.get("lunarCalenderMonthDay")).replace(memoConfig.getLeapMonthIdentifier(), ""))) {
                        stringObjectMap.put("haveMemo", true);
                        memos.add(memo);
                    }
                }
                if (memo.getCalendarType() == 1) {
                    //新历
                    if (StringUtils.equals((month < 10 ? "0" + month : month) + "-" + (finalI < 10 ? "0" + finalI : finalI), memoDateFormat)) {
                        stringObjectMap.put("haveMemo", true);
                        memos.add(memo);
                    }
                }
                stringObjectMap.put("memo", memos);
            });
            result.put(finalI, stringObjectMap);
        }
        return result;
    }

    private void setMemoTimes(Memo memo){
        List<MemoTimes> allMemoTimes = memoTimesService.getAllMemoTimes();
        setMemoTimes(memo,allMemoTimes);
    }
    private void setMemoTimes(Memo memo,List<MemoTimes> allMemoTimes){
        Integer remindTimes = memo.getRemindTimes();
        List<Integer> times = new ArrayList<>();
        Iterator<MemoTimes> iterator = allMemoTimes.iterator();
        while (iterator.hasNext()) {
            MemoTimes time = iterator.next();
            Integer daysNo = time.getDaysNo();
            if ((daysNo & remindTimes) == daysNo){
                times.add(daysNo);
            }
        }
        memo.setTimes(times);
    }
}
